package com.wcpdoc.exam.core.dao.impl;

import com.wcpdoc.core.dao.impl.RBaseDaoImpl;
import com.wcpdoc.core.entity.PageIn;
import com.wcpdoc.core.entity.PageOut;
import com.wcpdoc.core.entity.PageResult;
import com.wcpdoc.core.entity.PageResultEx;
import com.wcpdoc.core.util.SqlUtil;
import com.wcpdoc.core.util.ValidateUtil;
import com.wcpdoc.exam.core.dao.UserQuestionTypeDao;
import com.wcpdoc.exam.core.entity.UserQuestionType;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class UserQuestionTypeDaoImpl extends RBaseDaoImpl<UserQuestionType> implements UserQuestionTypeDao {
    @Override
    public PageOut getListpage(PageIn pageIn) {
//        String sql = "select b.ID, b.NAME, b.LOGIN_NAME, b.ORG_ID, c.NAME AS ORG_NAME,b.STATE " +
//                "from exm_user_question_type  a " +
//                "inner join SYS_USER b on a.USER_ID=b.ID " +
//                "LEFT JOIN SYS_ORG c ON b.ORG_ID = c.ID " +
//                "WHERE a.STATE=1 ";
        String sql = "select b.ID, b.NAME, b.LOGIN_NAME, b.ORG_ID, c.NAME AS ORG_NAME,b.STATE " +
                "from exm_user_question_type  a " +
                "inner join SYS_USER b on a.USER_ID=b.ID " +
                "LEFT JOIN SYS_ORG c ON b.ORG_ID = c.ID " ;
        SqlUtil sqlUtil = new SqlUtil(sql);
        sqlUtil.addWhere(ValidateUtil.isValid(pageIn.get("questionTypeId")), "a.QUESTION_TYPE_ID = :QUESTION_TYPE_ID", pageIn.get("questionTypeId"))
                .addWhere("a.STATE = 1");
        PageOut pageOut = getListpage(sqlUtil, pageIn);
        return pageOut;
    }

    @Override
    public List<Map<String,Object>> getUserByQuestionTypeId(Integer questionTypeId) {
        String sql = "select b.ID, b.NAME, b.LOGIN_NAME, b.ORG_ID, c.NAME AS ORG_NAME,b.STATE " +
                "from exm_user_question_type  a " +
                "inner join SYS_USER b on a.USER_ID=b.ID " +
                "LEFT JOIN SYS_ORG c ON b.ORG_ID = c.ID " +
                "WHERE a.STATE=1  and a.QUESTION_TYPE_ID=:questionTypeId";
        return getMapList(sql, new Object[]{questionTypeId});
    }

    /**
     * 删除题库对应的考试人员
     *
     * @param questionTypeId
     */
    @Override
    public void deleteByQuestionTypeId(Integer questionTypeId) {
        String sql = "delete from exm_user_question_type where QUESTION_TYPE_ID=:questionTypeId";
        update(sql, new Object[]{questionTypeId});
    }
}
